import { defineStore } from 'pinia'
import { getUserLikedPostIds, togglePostLike, getUserFavoritePostIds, togglePostFavorite } from '@/api/post/post'

export interface PostState {
	userLikedPostIds: number[]
	userFavoritePostIds: number[] // 添加收藏状态
	isLoading: boolean
}

export const usePostStore = defineStore('post', {
	state: (): PostState => ({
		userLikedPostIds: [], // 用户点赞的帖子ID列表
		userFavoritePostIds: [], // 用户收藏的帖子ID列表
		isLoading: false // 加载状态
	}),
	
	getters: {
		// 检查指定帖子是否被用户点赞
		isPostLiked: (state) => {
			return (postId: number): boolean => {
				return state.userLikedPostIds.includes(postId)
			}
		},
		
		// 检查指定帖子是否被用户收藏
		isPostFavorite: (state) => {
			return (postId: number): boolean => {
				return state.userFavoritePostIds.includes(postId)
			}
		},
		
		// 获取用户点赞的帖子数量
		likedPostsCount: (state): number => {
			return state.userLikedPostIds.length
		},
		
		// 获取用户收藏的帖子数量
		favoritePostsCount: (state): number => {
			return state.userFavoritePostIds.length
		}
	},
	
	actions: {
		// 获取用户点赞的帖子ID列表
		async fetchUserLikedPosts() {
			this.isLoading = true
			try {
				const res = await getUserLikedPostIds()
				
				if (res && res.data && Array.isArray(res.data)) {
					this.userLikedPostIds = res.data
				} else {
					this.userLikedPostIds = []
				}
			} catch (error) {
				console.error('获取用户点赞帖子失败：', error)
				this.userLikedPostIds = []
			} finally {
				this.isLoading = false
			}
		},
		
		// 切换帖子点赞状态
		async toggleLike(postId: number) {
			try {
				const res = await togglePostLike(postId)
				
				if (res && res.code === 200) {
					// 根据当前状态更新本地数据
					const index = this.userLikedPostIds.indexOf(postId)
					if (index > -1) {
						// 如果已点赞，则移除
						this.userLikedPostIds.splice(index, 1)
					} else {
						// 如果未点赞，则添加
						this.userLikedPostIds.push(postId)
					}
					return true
				} else {
					console.error('点赞操作失败:', res)
					return false
				}
			} catch (error) {
				console.error('点赞操作异常:', error)
				return false
			}
		},
		
		// 添加点赞（用于本地状态更新）
		addLike(postId: number) {
			if (!this.userLikedPostIds.includes(postId)) {
				this.userLikedPostIds.push(postId)
			}
		},
		
		// 移除点赞（用于本地状态更新）
		removeLike(postId: number) {
			const index = this.userLikedPostIds.indexOf(postId)
			if (index > -1) {
				this.userLikedPostIds.splice(index, 1)
			}
		},
		
		// 获取用户收藏的帖子ID列表
		async fetchUserFavoritePosts() {
			this.isLoading = true
			try {
				const res = await getUserFavoritePostIds()
				
				if (res && res.data && Array.isArray(res.data)) {
					this.userFavoritePostIds = res.data
				} else {
					this.userFavoritePostIds = []
				}
			} catch (error) {
				console.error('获取用户收藏帖子失败：', error)
				this.userFavoritePostIds = []
			} finally {
				this.isLoading = false
			}
		},
		
		// 切换帖子收藏状态
		async toggleFavorite(postId: number) {
			try {
				const res = await togglePostFavorite(postId)
				
				if (res && res.code === 200) {
					// 根据当前状态更新本地数据
					const index = this.userFavoritePostIds.indexOf(postId)
					if (index > -1) {
						// 如果已收藏，则移除
						this.userFavoritePostIds.splice(index, 1)
					} else {
						// 如果未收藏，则添加
						this.userFavoritePostIds.push(postId)
					}
					return true
				} else {
					console.error('收藏操作失败:', res)
					return false
				}
			} catch (error) {
					console.error('收藏操作异常:', error)
					return false
				}
		},
		
		// 添加收藏（用于本地状态更新）
		addFavorite(postId: number) {
			if (!this.userFavoritePostIds.includes(postId)) {
				this.userFavoritePostIds.push(postId)
			}
		},
		
		// 移除收藏（用于本地状态更新）
		removeFavorite(postId: number) {
			const index = this.userFavoritePostIds.indexOf(postId)
			if (index > -1) {
				this.userFavoritePostIds.splice(index, 1)
			}
		},
		
		// 清空用户收藏数据（用于登出时）
		clearUserFavoritePosts() {
			this.userFavoritePostIds = []
		},
		
		// 修改原有的清空方法
		clearUserLikedPosts() {
			this.userLikedPostIds = []
			this.userFavoritePostIds = []
			this.isLoading = false
		}
	}
})